import { RouterProvider, createRouter } from "@tanstack/solid-router";
import { render } from "solid-js/web";
import { routeTree } from "./routeTree.gen";
import "./styles.css";
{{#if (eq api "orpc")}}
import { QueryClientProvider } from "@tanstack/solid-query";
import { orpc, queryClient } from "./utils/orpc";
{{/if}}

const router = createRouter({
  routeTree,
  defaultPreload: "intent",
  scrollRestoration: true,
  defaultPreloadStaleTime: 0,
  {{#if (eq api "orpc")}}
  context: { orpc, queryClient },
  {{/if}}
});

declare module "@tanstack/solid-router" {
  interface Register {
    router: typeof router;
  }
}

function App() {
  return (
    {{#if (eq api "orpc")}}
    <QueryClientProvider client={queryClient}>
    {{/if}}
      <RouterProvider router={router} />
    {{#if (eq api "orpc")}}
    </QueryClientProvider>
    {{/if}}
  );
}

const rootElement = document.getElementById("app");
if (rootElement) {
  render(() => <App />, rootElement);
}
